local path "C:\Users\pgi1\Dropbox\Coal 2021 _ Joe Peter\Bank Regressions\"
cd "`path'\data"
set seed 100
set more off
clear



use  loan_year_vars_dataset, clear

//edit if lender=="BofA Securities"

bysort ultimateparentid year: keep if _n==1

xtset ultimateparentid year



gen treated_m10=(year==minpolicyyear-9)
gen treated_m9=(year==minpolicyyear-8)
gen treated_m8=(year==minpolicyyear-7)
gen treated_m7=(year==minpolicyyear-6)
gen treated_m6=(year==minpolicyyear-5)
gen treated_m5=(year==minpolicyyear-4)
gen treated_m4=(year==minpolicyyear-3)
gen treated_m3=(year==minpolicyyear-2)
gen treated_m2=(year==minpolicyyear-1)
gen treated_m1=(year==minpolicyyear)
gen treated_0=(year==minpolicyyear+1)
gen treated_1=(year==minpolicyyear+2)
gen treated_2=(year==minpolicyyear+3)
gen treated_3=(year==minpolicyyear+4)
gen treated_4=(year==minpolicyyear+5)
gen treated_5=(year==minpolicyyear+6)
gen treated_6=(year==minpolicyyear+7)
gen treated_7=(year==minpolicyyear+8)
gen treated_8=(year==minpolicyyear+9)
gen treated_9=(year==minpolicyyear+10)
gen treated_10=(year==minpolicyyear+11)

gen x= year if treated==1
bysort ultimateparentid : egen min_Year_tr=min(x)

keep if year>=2000 & year<=2020



est clear

xtreg loans_mtr_ln  treated_m5-treated_m1 treated_0-treated_5   log_assets asset_growth_3 loans_total_lag3_ln  loans_related_lag3_ln y_*, fe vce(cluster ultimateparentid)

gen est=.
replace est=_b[treated_m5] if _n==1
replace est=_b[treated_m4] if _n==2
replace est=_b[treated_m3] if _n==3
replace est=_b[treated_m2] if _n==4
replace est=_b[treated_m1] if _n==5
replace est=_b[treated_0] if _n==6
replace est=_b[treated_1] if _n==7
replace est=_b[treated_2] if _n==8
replace est=_b[treated_3] if _n==9
replace est=_b[treated_4] if _n==10
replace est=_b[treated_5] if _n==11

gen se=.
replace se=_se[treated_m5] if _n==1
replace se=_se[treated_m4] if _n==2
replace se=_se[treated_m3] if _n==3
replace se=_se[treated_m2] if _n==4
replace se=_se[treated_m1] if _n==5
replace se=_se[treated_0] if _n==6
replace se=_se[treated_1] if _n==7
replace se=_se[treated_2] if _n==8
replace se=_se[treated_3] if _n==9
replace se=_se[treated_4] if _n==10
replace se=_se[treated_5] if _n==11

gen time=_n-6

keep est se time
keep if _n<=11

gen est_p5=est-2*se
gen est_p95=est+2*se


twoway (scatter   est_p5 time , connect(l) lpattern(shortdash) msymbol(S) color(grey)  lwidth(medthick)) /// 
(scatter est  time , connect(l) lpattern(solid) msymbol(S) color(red)  lwidth(medthick)) /// 
(scatter est_p95  time , connect(l) lpattern(shortdash) msymbol(S) color(grey)  lwidth(medthick)) /// 
, scheme(s2mono) graphregion(fcolor(white)) bgcolor(white)  xlabel(-5(1)5) xtitle("Event Time") ytitle("Estimate") ///
ysize(3.75) xsize(5) legend(off)


gen x1=-0.2
gen x2=0
gen x3=0.2
gen x4=0.4


twoway (rarea est_p5 est_p95 time, color(gs15)) /// 
(scatter est  time , connect(l) lpattern(solid) msymbol(S) color(red)   lwidth(medthick)) /// 
(scatter x1 time , connect(l) lpattern(shortdash)  color(gs10) msymbol(i)  lwidth(medthick)) /// 
(scatter x2 time , connect(l) lpattern(shortdash)  color(gs10) msymbol(i)  lwidth(medthick)) /// 
(scatter x3 time , connect(l) lpattern(shortdash)  color(gs10) msymbol(i)  lwidth(medthick)) /// 
(scatter x4 time , connect(l) lpattern(shortdash)  color(gs10) msymbol(i)  lwidth(medthick)) /// 
, scheme(s2mono) graphregion(fcolor(white)) bgcolor(white) ylab(-.2 "-0.2" 0 0.2 "0.2" 0.4 "0.4", nogrid) xlabel(-5(1)5) xtitle("Event Time") ytitle("Estimate") ///
ysize(3.75) xsize(5) legend(off)


graph export  "`path'\figures\figure3.emf", as(emf) replace


exit
